package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.TicketDao;
import com.bdqn.oa.entity.Ticket;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author qiaolu
 * @date 2022/7/20 17:40
 */
public class TicketDaolmpl implements TicketDao {
    private QueryRunner queryRunner= new QueryRunner();
    /**
     * 通过Ticketname查询
     *
     * @param TicketName
     * @return
     * @throws SQLException
     */
    @Override
    public List<Ticket> listbyidVoteOption(String TicketName) throws SQLException {
        List<Ticket> ticket = null;
        try {
            String sql = "select * from ticket where name = ?";
            ticket = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Ticket>(Ticket.class),TicketName);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ticket;
    }

    /**
     * 查询全部投票
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Ticket> listAllTicket() throws SQLException {

        List<Ticket> ticket = null;
        try {
            String sql = "select * from ticket ";
            ticket = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Ticket>(Ticket.class));
        }catch (Exception e){
            e.printStackTrace();
        }

        return ticket;
    }

    /**
     * 增加投票
     *
     * @param ticket
     * @return
     * @throws SQLException
     */
    @Override
    public int saveTicket(Ticket ticket) throws SQLException {
        String sql = "insert into ticket(name,option_id)values(?,?)";
        return queryRunner.update(DruidUtils.getConnection(),sql,ticket.getName(),ticket.getOptionId());
    }

    /**
     * 修改投票选项
     *
     * @param ticket
     * @return
     * @throws SQLException
     */
    @Override
    public int updateTicket(Ticket ticket) throws SQLException {
        String sql ="update ticket set name=?,option_id=? where id= ?";
        return queryRunner.update(DruidUtils.getConnection(),sql,ticket.getName(),ticket.getOptionId());
    }
}
